/**
 * 
 */
package com.arx.script;

import com.arx.common.GlobalConsts;
import com.arx.interactive.Interactive;
import com.arx.interactive.InteractiveObject;
import com.arx.script.flyweight.StackedEvent;

/**
 * @author Donald
 */
abstract class ScriptEventStackBase extends ScriptCommonVars {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_SCRIPT_EventStackClear() {
		for (short i = 0; i < GlobalConsts.MAX_EVENT_STACK; i++) {
			StackedEvent event = super.getEventstack(i);
			if (event != null && event.exists()) {
				event.setSender(null);
				event.setExists(false);
				event.setIo(null);
				event.setMsg(0);
				event.setParams(null);
				event.setEventname(null);
			}
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_SCRIPT_EventStackClearForIo(final InteractiveObject io) {
		for (short i = 0; i < GlobalConsts.MAX_EVENT_STACK; i++) {
			StackedEvent event = super.getEventstack(i);
			if (event != null && event.exists()) {
				if (event.getIo().equals(io)) {
					event.setSender(null);
					event.setExists(false);
					event.setIo(null);
					event.setMsg(0);
					event.setParams(null);
					event.setEventname(null);
				}
			}
		}
	}
	/**
	 * {@inheritDoc}
	 */
	public final void ARX_SCRIPT_EventStackExecute() {
		long count = 0;
		for (short i = 0; i < GlobalConsts.MAX_EVENT_STACK; i++) {
			StackedEvent event = super.getEventstack(i);
			if (event.exists()) {
				if (Interactive.getInstance().ValidIOAddress(
						event.getIo())) {
					if (Interactive.getInstance().ValidIOAddress(
							event.getSender())) {
						super.setEVENT_SENDER(event.getSender());
					} else {
						super.setEVENT_SENDER(null);
					}
					SendIOScriptEvent(event.getIo(),
							event.getMsg(), event.getParams(),
							event.getEventname());
				}
				event.setSender(null);
				event.setExists(false);
				event.setIo(null);
				event.setMsg(0);
				event.setParams(null);
				event.setEventname(null);
				count++;

				if (count >= super.getSTACK_FLOW()) {
					break;
				}
			}
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_SCRIPT_EventStackExecuteAll() {
		super.setSTACK_FLOW(9999999);
		ARX_SCRIPT_EventStackExecute();
		super.setSTACK_FLOW(20);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_SCRIPT_EventStackInit() throws Exception {
		for (short i = 0; i < GlobalConsts.MAX_EVENT_STACK; i++) {
			if (super.getEventstack(i) == null) {
				StackedEvent event = new StackedEvent();
				event.setSender(null);
				event.setExists(false);
				event.setIo(null);
				event.setMsg(0);
				event.setParams(null);
				event.setEventname(null);
				super.setEventstack(i, event);
			} else {
				StackedEvent event = super.getEventstack(i);
				event.setSender(null);
				event.setExists(false);
				event.setIo(null);
				event.setMsg(0);
				event.setParams(null);
				event.setEventname(null);
			}
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void Stack_SendIOScriptEvent(final InteractiveObject io,
			final long msg, final String params, final String eventname) {
		for (short i = 0; i < GlobalConsts.MAX_EVENT_STACK; i++) {
			StackedEvent event = super.getEventstack(i);
			if (event != null
					&& event.exists()) {
				if (params != null && params.length() > 0) {
					event.setParams(params);
				} else {
					event.setParams(null);
				}

				if (eventname != null && eventname.length() > 0) {
					event.setEventname(eventname);
				} else {
					event.setEventname(null);
				}
				event.setSender(super.getEVENT_SENDER());
				event.setExists(true);
				event.setIo(io);
				event.setMsg(msg);
				break;
			}
		}
	}
	/**
	 * Sends a message to all NPCs.
	 * @param msg the message
	 * @param dat the data parameters
	 */
	protected final void Stack_SendMsgToAllNPC_IO(final long msg, final String dat) {
		for (int i = 0; i < Interactive.getInstance().getNbmax(); i++) {
			InteractiveObject io = Interactive.getInstance()
					.getInteractiveObject(i);
			if (io != null && io.hasIoFlag(GlobalConsts.IO_NPC)) {
				Stack_SendIOScriptEvent(io, msg, dat, null);
			}
		}
	}
}
